﻿
using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Overseas.Servers
{
    public class CaseServers
    {
        private readonly IRepository<overseas_case> caseRepository;

        public CaseServers()
        {
            caseRepository = new DapperRepository<overseas_case>();
        }

        public IEnumerable<overseas_case> GetOverseasCaseList()
        {
            string sql = "SELECT * FROM [OADB].[dbo].[overseas_case]";
            return caseRepository.SqlWithParams(sql);
        }

        //public List<overseas_case> GetData(string userNO,string SUBJECT, string DOC_NUM, string M_START, string M_END)
        //{
        //    string sql = string.Format("SELECT * FROM [OADB].[dbo].[overseas_case] WHERE [DRAFT_AUTHOR]='{0}' OR [DEPT_LEADER_USER_NO]='{0}' OR [SIGNER_USER_NO]='{0}'", userNO);            
        //    var datas = caseRepository.SqlWithParams(sql);
        //    //if (!string.IsNullOrEmpty(SUBJECT))
        //    //{
        //    //    datas = datas.Where(a => a.SUBJECT.Contains(SUBJECT));
        //    //}
        //    //if (!string.IsNullOrEmpty(DOC_NUM))
        //    //{
        //    //    datas = datas.Where(a => a.DOC_NUM.Contains(DOC_NUM));
        //    //}
        //    //if (!string.IsNullOrEmpty(M_START))
        //    //{
        //    //    datas = datas.Where(a => a.SIGNATURE_TIME >= Convert.ToDateTime(M_START));
        //    //}
        //    //if (!string.IsNullOrEmpty(M_END))
        //    //{
        //    //    datas = datas.Where(a => a.SIGNATURE_TIME < Convert.ToDateTime(M_END));
        //    //}
        //    return datas.ToList();
        //}

        public overseas_case GetModelById(string id)
        {
            return caseRepository.GetById(id);
        }


        internal void Add(overseas_case model)
        {
            caseRepository.Insert(model);
        }

        internal void Update(overseas_case model)
        {
            caseRepository.Update(model);
        }

        internal void UpdateStatus(int id, int status)
        {
            string sql = string.Format("UPDATE [dbo].[overseas_case] SET [STATE] = '{0}' WHERE [DOCID] = {1}", status, id);
            caseRepository.SqlWithParams(sql);
        }
        
        internal void Del(string id)
        {
            caseRepository.DeleteByIds(id);
        }
        internal void UpdateArchiveStatus(int id)
        {
            string sql = string.Format("UPDATE [dbo].[overseas_case] SET [STATE] = '1' WHERE [ID] = {0}", id);
            caseRepository.SqlWithParams(sql);
        }
    }
}